package kr.co.creativeideas.gooq.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.view.SurfaceHolder.Callback;

public class IntroView extends SurfaceView implements Callback {

	static boolean mRun = false;

	private Context mContext;
	private SurfaceHolder mSurfaceHolder;
	private IntroViewThread mThread;
	private Drawable introImg;
	private int x, y, sizeX, sizeY;

	public IntroView(Context context) {
		// TODO Auto-generated constructor stub
		super(context);
	}

	public IntroView(Context context, AttributeSet attrs) {
		// TODO Auto-generated constructor stub
		super(context, attrs);
		SurfaceHolder holder = getHolder();
		holder.addCallback(this);
		mSurfaceHolder = holder;
		mContext = context;
		mThread = new IntroViewThread();
		init();
	}

	private void init() {
		Display display = ((WindowManager) mContext.getSystemService(mContext.WINDOW_SERVICE)).getDefaultDisplay();
		int width = display.getWidth();
		int height = display.getHeight();

		// Log.i("getWidth", "width = "+width);
		// Log.i("getHeight", "width = "+height);

		x = 0;
		y = 0;
		sizeX = width;
		sizeY = height;
		introImg = mContext.getResources().getDrawable(kr.co.creativeideas.gooq.activity.R.drawable.intro);
		introImg.setBounds(x, y, sizeX, sizeY);
	}

	public void surfaceChanged() {

	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		mThread.setRunning(true);
		mThread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		boolean retry = true;
		mThread.setRunning(false);
		// TODO Auto-generated method stub
		while (retry) {
			try {
				mThread.join();
				retry = false;
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void doDraw(Canvas canvas) {
		canvas.save();
		//canvas.rotate(90, 480, 0);
		introImg.draw(canvas);
	}

	class IntroViewThread extends Thread {
		Bitmap introImg;

		public IntroViewThread() {
			Display display = ((WindowManager) mContext.getSystemService(mContext.WINDOW_SERVICE)).getDefaultDisplay();
			int width = display.getWidth();
			int height = display.getHeight();
			introImg = BitmapFactory.decodeResource(mContext.getResources(),	kr.co.creativeideas.gooq.activity.R.drawable.intro);
			introImg = Bitmap.createScaledBitmap(introImg, width, height, true);
		}

		public void run() {
			while (mRun) {
				Canvas canvas = null;
				canvas = mSurfaceHolder.lockCanvas();
				try {
					synchronized (mSurfaceHolder) {
						// canvas.drawBitmap(introImg, 0, 0, null);
						doDraw(canvas);
						sleep(100);
					}
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				} finally {
					if (canvas != null) {
						mSurfaceHolder.unlockCanvasAndPost(canvas);
					}
				}
			}
		}

		public void setRunning(boolean run) {
			mRun = run;
		}
	}
}